<?php

namespace app\admin\controller\yalimedia;

use app\common\controller\Backend;

use app\common\model\CourseQuestion as CourseQuestionModel;



/**
 * 课程题库列表
 *
 * @icon fa fa-circle-o
 */
class Coursequestion extends Backend
{

    protected $model = null;
    protected $modelCate = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new  CourseQuestionModel;
    }

    
    /**
     * 查看
     */
    public function index()
    {
        $ids = input('ids');

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            $list = $this->model
                ->with('question')
                ->where($where)
                ->where(['cate_id'=>$ids])
                ->order($sort, $order)
                ->paginate($limit);
      
            foreach ($list as $value) {
                if(empty($value['question'])){
                    $this->model->where('id',$value['id'])->delete();
                }
            }
            
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }

        $this->assign('ids',$ids);
        return $this->view->fetch();
    }

    
    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        $row = $this->model->get($ids);
        $this->modelValidate = true;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        
        $this->model->where('id','IN',$ids)->delete();
        $this->success();
    }


}
